#include "TimerComponent.h"

void TimerComponent::showAll()
{
	updateHour = true;
	updateMin = true;
	updateSec = true;
}

void TimerComponent::updateTime(uint8_t& hour, uint8_t& min, uint8_t& sec, uint8_t& millsec)
{
	updateHour = false;
	updateMin = false;
	updateSec = false;
	
	if (this->hour != hour)
	{
		updateHour = true;
		this->hour = hour;
	}

	if (this->min != min)
	{
		updateMin = true;
		this->min = min;
	}

	if (this->sec != sec)
	{
		updateSec = true;
		this->sec = sec;
	}

	this->millsec = millsec;

	render();
}

void TimerComponent::clearArea()
{
	if (updateHour)
	{
		lcd.fillRect(TIMER_TEXT_POS_X, TIMER_TEXT_POS_Y,
			TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_HEIGHT, TIMER_BACKGROUND_COLOR);
	}
	if (updateMin)
	{
		lcd.fillRect(TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_POS_Y,
			TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_HEIGHT, TIMER_BACKGROUND_COLOR);
	}
	if (updateSec)
	{
		lcd.fillRect(TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH * 2, TIMER_TEXT_POS_Y,
			TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_HEIGHT, TIMER_BACKGROUND_COLOR);
	}

	lcd.fillRect(TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH * 3, TIMER_TEXT_POS_Y,
		TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_HEIGHT, TIMER_BACKGROUND_COLOR);
}

void TimerComponent::doRender()
{

	lcd.setFreeFont(TIMER_TEXT_FONT);
	lcd.setTextColor(TIMER_TEXT_COLOR);

	if (updateHour)
	{
		char seg[3];
		sprintf(seg, "%02d:", hour);
		lcd.drawString(seg, TIMER_TEXT_POS_X, TIMER_TEXT_POS_Y);
	}
	if (updateMin)
	{
		char seg[3];
		sprintf(seg, "%02d:", min);
		lcd.drawString(seg, TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH, TIMER_TEXT_POS_Y);
	}
	if (updateSec)
	{
		char seg[3];
		sprintf(seg, "%02d.", sec);
		lcd.drawString(seg, TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH * 2, TIMER_TEXT_POS_Y);
	}

	char seg[2];
	sprintf(seg, "%02d", millsec);
	lcd.drawString(seg, TIMER_TEXT_POS_X + TIMER_TEXT_SEG_WITDTH * 3, TIMER_TEXT_POS_Y);
}